package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.WmsMaterialInfo;
import com.ruoyi.system.mapper.MesProcessModelMapper;
import com.ruoyi.system.domain.MesProcessModel;
import com.ruoyi.system.service.IMesProcessModelService;

/**
 * 工艺建模Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-01
 */
@Service
public class MesProcessModelServiceImpl implements IMesProcessModelService 
{
    @Autowired
    private MesProcessModelMapper mesProcessModelMapper;

    /**
     * 查询工艺建模
     * 
     * @param id 工艺建模主键
     * @return 工艺建模
     */
    @Override
    public MesProcessModel selectMesProcessModelById(Long id)
    {
        return mesProcessModelMapper.selectMesProcessModelById(id);
    }

    /**
     * 查询工艺建模列表
     * 
     * @param mesProcessModel 工艺建模
     * @return 工艺建模
     */
    @Override
    public List<MesProcessModel> selectMesProcessModelList(MesProcessModel mesProcessModel)
    {
        return mesProcessModelMapper.selectMesProcessModelList(mesProcessModel);
    }

    /**
     * 新增工艺建模
     * 
     * @param mesProcessModel 工艺建模
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesProcessModel(MesProcessModel mesProcessModel)
    {
        int rows = mesProcessModelMapper.insertMesProcessModel(mesProcessModel);
        insertWmsMaterialInfo(mesProcessModel);
        return rows;
    }

    /**
     * 修改工艺建模
     * 
     * @param mesProcessModel 工艺建模
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesProcessModel(MesProcessModel mesProcessModel)
    {
        mesProcessModelMapper.deleteWmsMaterialInfoById(mesProcessModel.getId());
        insertWmsMaterialInfo(mesProcessModel);
        return mesProcessModelMapper.updateMesProcessModel(mesProcessModel);
    }

    /**
     * 批量删除工艺建模
     * 
     * @param ids 需要删除的工艺建模主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesProcessModelByIds(Long[] ids)
    {
        mesProcessModelMapper.deleteWmsMaterialInfoByIds(ids);
        return mesProcessModelMapper.deleteMesProcessModelByIds(ids);
    }

    /**
     * 删除工艺建模信息
     * 
     * @param id 工艺建模主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesProcessModelById(Long id)
    {
        mesProcessModelMapper.deleteWmsMaterialInfoById(id);
        return mesProcessModelMapper.deleteMesProcessModelById(id);
    }

    /**
     * 新增物料档案信息
     * 
     * @param mesProcessModel 工艺建模对象
     */
    public void insertWmsMaterialInfo(MesProcessModel mesProcessModel)
    {
        List<WmsMaterialInfo> wmsMaterialInfoList = mesProcessModel.getWmsMaterialInfoList();
        Long id = mesProcessModel.getId();
        if (StringUtils.isNotNull(wmsMaterialInfoList))
        {
            List<WmsMaterialInfo> list = new ArrayList<WmsMaterialInfo>();
            for (WmsMaterialInfo wmsMaterialInfo : wmsMaterialInfoList)
            {
                wmsMaterialInfo.setId(id);
                list.add(wmsMaterialInfo);
            }
            if (list.size() > 0)
            {
                mesProcessModelMapper.batchWmsMaterialInfo(list);
            }
        }
    }
}
